package com.example.jf.jfapp.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;

/**
 * Created by jf on 2017/7/30.
 */

public  abstract  class CommentReceiver extends BroadcastReceiver {
    /* 新评论的数据，可以理为刚刚发送的评论数据 */
    public final static String NEW_COMMENT_KEY = "new_comment_key";
//
//    @Override
//    public void onReceive(Context context, Intent intent) {
//
//    }

    public class Action {
        //评论一条动态成功后，会发送该广播
        public final static String ACTION_COMMENT_SUCCESSFUL = "com.jf.ACTION_COMMENT_SUCCESSFUL";
    }

    /**
     * 便捷方法
     * @param context
     * @param callback
     */
    public static void registerReceiver(Context context, final CommentChangeCallback callback){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CommentReceiver.Action.ACTION_COMMENT_SUCCESSFUL);
        LocalBroadcastManager.getInstance(context).registerReceiver(new CommentReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //获取发送过来的评论数据
                CommentReceiver.CommentBean commentBean = intent.getParcelableExtra(CommentReceiver.NEW_COMMENT_KEY);
                callback.changCallback(commentBean);
            }
        },intentFilter);
    }


    public interface CommentChangeCallback{
        public void changCallback(CommentReceiver.CommentBean commentBean);
    }



    /**
     * 评论数据
     */
    public static class CommentBean implements Parcelable {
        private String content;
        private String createdAt;
        private String objectId;
        private UserBean user;
        private String statuId;

        public CommentBean(){}

        protected CommentBean(Parcel in) {
            content = in.readString();
            createdAt = in.readString();
            objectId = in.readString();
            statuId = in.readString();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(content);
            dest.writeString(createdAt);
            dest.writeString(objectId);
            dest.writeString(statuId);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public static final Creator<CommentBean> CREATOR = new Creator<CommentBean>() {
            @Override
            public CommentBean createFromParcel(Parcel in) {
                return new CommentBean(in);
            }

            @Override
            public CommentBean[] newArray(int size) {
                return new CommentBean[size];
            }
        };

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getCreatedAt() {
            return createdAt;
        }

        public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
        }

        public String getObjectId() {
            return objectId;
        }

        public void setObjectId(String objectId) {
            this.objectId = objectId;
        }

        public UserBean getUser() {
            return user;
        }

        public void setUser(UserBean user) {
            this.user = user;
        }

        public static class UserBean {
            /**
             * objectId : 594cc574570c357d06925a54
             */
            private String objectId;

            public String getObjectId() {
                return objectId;
            }

            public void setObjectId(String objectId) {
                this.objectId = objectId;
            }
        }

        public String getStatuId() {
            return statuId;
        }

        public void setStatuId(String statuId) {
            this.statuId = statuId;
        }
    }
}

